package com.lance.core.verticle;



import java.util.HashMap;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.lance.core.util.JacksonUtil;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.eventbus.DeliveryOptions;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.json.JsonObject;

public class SocketVerticle extends AbstractVerticle {
	public static final Logger log = LogManager.getLogger(SocketVerticle.class);
	@SuppressWarnings("unchecked")
	public void start(Future<Void> future){	
		try {
			 EventBus eventBus=vertx.eventBus();
			 //设置数据推送出去的时间限制
		     DeliveryOptions deliveryOptions=new DeliveryOptions(new JsonObject().put("timeout", 5000));
		     //注册地址,然后对接收到客户端来的消息进行处理
			 eventBus.consumer("chat.to.server", message -> {
		          HashMap<String, String> messageObject = (HashMap<String, String>) JacksonUtil.strToObject(message.body().toString(), HashMap.class);
		          String messageTo = messageObject.get("to");
		          messageObject.remove("to");
		          eventBus.publish("server.to.chat"+messageTo, JacksonUtil.objectToStr(messageObject) ,deliveryOptions);
	         });
			
			log.info("httpserver listenning on port : "+config().getInteger("http.port",8080));
		} catch (Exception e) {
			log.error("服务启动异常",e);
		}
	}
	
}
